package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CatalogMapper;
import com.ruoyi.system.domain.Catalog;
import com.ruoyi.system.service.ICatalogService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 目录管理Service业务层处理
 * 
 * @author tq
 * @date 2025-01-07
 */
@Service
public class CatalogServiceImpl implements ICatalogService 
{
    @Autowired
    private CatalogMapper catalogMapper;

    /**
     * 查询目录管理
     * 
     * @param catalogId 目录管理主键
     * @return 目录管理
     */
    @Override
    public Catalog selectCatalogByCatalogId(Long catalogId)
    {
        return catalogMapper.selectCatalogByCatalogId(catalogId);
    }

    /**
     * 查询目录管理列表
     * 
     * @param catalog 目录管理
     * @return 目录管理
     */
    @Override
    public List<Catalog> selectCatalogList(Catalog catalog)
    {
        return catalogMapper.selectCatalogList(catalog);
    }

    /**
     * 新增目录管理
     * 
     * @param catalog 目录管理
     * @return 结果
     */
    @Override
    public int insertCatalog(Catalog catalog)
    {
        catalog.setCreateTime(DateUtils.getNowDate());
        catalog.setCreateBy(getUsername());
        return catalogMapper.insertCatalog(catalog);
    }

    /**
     * 修改目录管理
     * 
     * @param catalog 目录管理
     * @return 结果
     */
    @Override
    public int updateCatalog(Catalog catalog)
    {
        catalog.setUpdateTime(DateUtils.getNowDate());
        catalog.setUpdateBy(getUsername());
        return catalogMapper.updateCatalog(catalog);
    }

    /**
     * 批量删除目录管理
     * 
     * @param catalogIds 需要删除的目录管理主键
     * @return 结果
     */
    @Override
    public int deleteCatalogByCatalogIds(Long[] catalogIds)
    {
        return catalogMapper.deleteCatalogByCatalogIds(catalogIds);
    }

    /**
     * 删除目录管理信息
     * 
     * @param catalogId 目录管理主键
     * @return 结果
     */
    @Override
    public int deleteCatalogByCatalogId(Long catalogId)
    {
        return catalogMapper.deleteCatalogByCatalogId(catalogId);
    }

    /**
     * 导入目录数据
     * 
     * @param catalogList 目录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importCatalog(List<Catalog> catalogList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(catalogList) || catalogList.size() == 0)
        {
            throw new RuntimeException("导入目录数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Catalog catalog : catalogList)
        {
            try
            {
                // 验证是否存在这个目录（根据题名和年度判断）
//                Catalog existsCatalog = new Catalog();
//                existsCatalog.setTitle(catalog.getTitle());
//                existsCatalog.setYear(catalog.getYear());
//                List<Catalog> existsList = catalogMapper.selectCatalogList(existsCatalog);
//                if (existsList.size() == 0)
//                {
//                    catalog.setCreateBy(operName);
//                    catalog.setCreateTime(DateUtils.getNowDate());
//                    this.insertCatalog(catalog);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、题名 " + catalog.getTitle() + " 导入成功");
//                }
//                else if (isUpdateSupport)
//                {
//                    catalog.setCatalogId(existsList.get(0).getCatalogId());
//                    catalog.setUpdateBy(operName);
//                    catalog.setUpdateTime(DateUtils.getNowDate());
//                    this.updateCatalog(catalog);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、题名 " + catalog.getTitle() + " 更新成功");
//                }
//                else
//                {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、题名 " + catalog.getTitle() + " 已存在");
//                }
                catalog.setCreateBy(operName);
                catalog.setCreateTime(DateUtils.getNowDate());
                this.insertCatalog(catalog);
                successNum++;
                successMsg.append("<br/>" + successNum + "、题名 " + catalog.getTitle() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、题名 " + catalog.getTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
